package com.lou.springboot.controller;

import com.lou.springboot.common.ResultVO;
import com.lou.springboot.common.ResultVOUtil;
import com.lou.springboot.entity.User;
import com.lou.springboot.enums.ResultEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/api")
public class ApiController {
    static Map<Integer, User> userMap = Collections.synchronizedMap(new HashMap<>());

    // 初始化 userMap
    static {
        User user = new User();
        user.setId(2);
        user.setName("user1");
        user.setPassword("123456");
        User user1 = new User();
        user1.setId(5);
        user1.setName("user5");
        user1.setPassword("123456");
        User user2 = new User();
        user2.setId(6);
        user2.setName("user6");
        user2.setPassword("123456");
        userMap.put(2, user);
        userMap.put(5, user1);
        userMap.put(6, user2);
    }

    // 查询一条记录
    @RequestMapping(value = "/users/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultVO getOne(@PathVariable Integer id){
        if (id == null && id < 1) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        User user = userMap.get(id);
        if (Objects.isNull(user)) {
            return ResultVOUtil.error(ResultEnum.NOT_FOUND);
        }
        return ResultVOUtil.success(user);
    }

    @GetMapping("/users")
    public ResultVO queryAll(){
        List<User> users = new ArrayList<>(userMap.values());
        return ResultVOUtil.success(users);
    }

    @PostMapping("/users")
    public ResultVO insert(@RequestBody User user){
        if (StringUtils.isEmpty(user.getId()) ||
            StringUtils.isEmpty(user.getName()) ||
            StringUtils.isEmpty(user.getPassword())){
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        if (userMap.containsKey(user.getId())) {
            return ResultVOUtil.error(ResultEnum.ID_IS_EXIST);
        }
        userMap.put(user.getId(), user);
        return ResultVOUtil.success();
    }

    @PutMapping("/users")
    public ResultVO update(@RequestBody User user){
        if (StringUtils.isEmpty(user.getId()) ||
            user.getId() < 1 ||
            StringUtils.isEmpty(user.getName()) ||
            StringUtils.isEmpty(user.getPassword())){
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        User user1 = userMap.get(user.getId());
        if (Objects.isNull(user1)) {
            return ResultVOUtil.error(ResultEnum.NOT_FOUND);
        }
        user1.setName(user.getName());
        user1.setPassword(user.getPassword());
        userMap.put(user.getId(), user1);
        return ResultVOUtil.success(user1);
    }

    @DeleteMapping("/users/{id}")
    public ResultVO update(@PathVariable Integer id){
        if (StringUtils.isEmpty(id) || id < 1){
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        User user = userMap.get(id);
        if (Objects.isNull(user)) {
            return ResultVOUtil.error(ResultEnum.NOT_FOUND);
        }
        userMap.remove(user.getId());
        return ResultVOUtil.success();
    }
}
